package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.ContentCollectionVo;
import org.dromara.business.domain.bo.ContentCollectionBo;
import org.dromara.business.service.IContentCollectionService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 统一内容合集
 *
 * @author Hao Tong Xue
 * @date 2025-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/contentCollection")
public class ContentCollectionController extends BaseController {

    private final IContentCollectionService contentCollectionService;

    /**
     * 查询统一内容合集列表
     */
//    @SaCheckPermission("business:contentCollection:list")
    @GetMapping("/list")
    public TableDataInfo<ContentCollectionVo> list(ContentCollectionBo bo, PageQuery pageQuery) {
        return contentCollectionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出统一内容合集列表
     */
    @SaCheckPermission("business:contentCollection:export")
    @Log(title = "统一内容合集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ContentCollectionBo bo, HttpServletResponse response) {
        List<ContentCollectionVo> list = contentCollectionService.queryList(bo);
        ExcelUtil.exportExcel(list, "统一内容合集", ContentCollectionVo.class, response);
    }

    /**
     * 获取统一内容合集详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:contentCollection:query")
    @GetMapping("/{id}")
    public R<ContentCollectionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(contentCollectionService.queryById(id));
    }

    /**
     * 新增统一内容合集
     */
//    @SaCheckPermission("business:contentCollection:add")
    @Log(title = "统一内容合集", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContentCollectionBo bo) {
        return toAjax(contentCollectionService.insertByBo(bo));
    }

    /**
     * 修改统一内容合集
     */
    @SaCheckPermission("business:contentCollection:edit")
    @Log(title = "统一内容合集", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContentCollectionBo bo) {
        return toAjax(contentCollectionService.updateByBo(bo));
    }

    /**
     * 删除统一内容合集
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:contentCollection:remove")
    @Log(title = "统一内容合集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(contentCollectionService.deleteWithValidByIds(List.of(ids), true));
    }
}
